package com.jueye.onlinetalk.service.impl;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jueye.onlinetalk.entry.User;
import com.jueye.onlinetalk.entry.UserRoleRel;
import com.jueye.onlinetalk.entry.bo.RoleBo;
import com.jueye.onlinetalk.entry.bo.UserBo;
import com.jueye.onlinetalk.entry.bo.UserRoleRelBo;
import com.jueye.onlinetalk.entry.vo.RoleVo;
import com.jueye.onlinetalk.mapper.RoleMapper;
import com.jueye.onlinetalk.mapper.UserMapper;
import com.jueye.onlinetalk.service.RoleService;
import com.jueye.onlinetalk.service.UserRoleRelService;
import com.jueye.onlinetalk.mapper.UserRoleRelMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author ASUS
* @description 针对表【user_role_rel(用户关联角色)】的数据库操作Service实现
* @createDate 2025-07-07 11:36:39
*/
@Service
public class UserRoleRelServiceImpl extends ServiceImpl<UserRoleRelMapper, UserRoleRel>
    implements UserRoleRelService{

    @Resource
    private UserRoleRelMapper userRoleRelMapper;
    public QueryWrapper<UserRoleRel> getQueryWrapper(UserRoleRelBo userRoleRelBo) {
        QueryWrapper<UserRoleRel> queryWrapper = new QueryWrapper<>();
        if (userRoleRelBo != null) {
            queryWrapper.in(userRoleRelBo.getUserIds() != null, "user_id", userRoleRelBo.getUserIds());
            queryWrapper.in(userRoleRelBo.getRoleIds() != null, "role_id", userRoleRelBo.getRoleIds());
        }
        return queryWrapper;
    }

    @Override
    public List<RoleVo> getRoleVosByUserId(Long userId) {
        UserRoleRelBo userRoleRelBo = new UserRoleRelBo();
        userRoleRelBo.setUserIds(List.of(userId));
        List<UserRoleRel> userRoleRels = userRoleRelMapper.selectList(this.getQueryWrapper(userRoleRelBo));
        RoleService roleService = SpringUtil.getBean(RoleServiceImpl.class);
        RoleBo roleBo = new RoleBo();
        roleBo.setIds(userRoleRels.stream().map(UserRoleRel::getRoleid).collect(Collectors.toList()));
        return roleService.getVos(roleBo);
    }
}




